package com.ruiysoft.controller.sys.rabbit;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.ruiysoft.conf.mq.PayCallBackMqConfig;
import com.ruiysoft.controller.wx.dto.OrderPaidDto;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.entity.sys.SysTransactionRecord;
import com.ruiysoft.service.sys.ISysMicroShopService;
import com.ruiysoft.service.sys.ISysTransactionRecordService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by 化增光 on 2018/11/24.
 */
@Component
public class SysMicroShopSubscribe {
    private static final Logger log = LoggerFactory.getLogger(SysMicroShopSubscribe.class);
    @Resource
    ISysTransactionRecordService iSysTransactionRecordService;

    @Resource
    ISysMicroShopService iSysMicroShopService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = PayCallBackMqConfig.PAY_CALLBACK_VIP_QUANTITY_QUEUE, durable = "true", autoDelete="true"),
                    exchange = @Exchange(value = PayCallBackMqConfig.PAY_CALLBACK_QUANTITY_EXCHANGE, type = ExchangeTypes.DIRECT),
                    key = PayCallBackMqConfig.PAY_CALLBACK_VIP_QUANTITY_BIND_KEY)
    )
    public void onMessage(Message message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        String jsonString = new String(message.getBody());
        log.info("店铺升级:"+jsonString);
       // System.out.println("监听店铺购买:---"+jsonString);
        try {
            OrderPaidDto dto = JSON.parseObject(jsonString,OrderPaidDto.class);
            if ("SUCCESS".equals(dto.getResultCode())){
                SysTransactionRecord record = iSysTransactionRecordService.getOne(new QueryWrapper<SysTransactionRecord>().eq("transaction_no",dto.getTransactionNo()));
                record.setTransactionId(dto.getTransactionId()).setPayTime(dto.getPayTime()).setState(1);
                iSysTransactionRecordService.updateById(record);

                SysMicroShop sysMicroShop = iSysMicroShopService.selectSysMicroShopById(record.getShopId());
                Date nowDate = new Date();
                Date expiryDate = sysMicroShop.getExpiryTime();
                if(expiryDate == null || expiryDate.compareTo(nowDate) < 0){
                    expiryDate = nowDate;
                }
                expiryDate = DateUtils.addMonths(expiryDate, record.getPurchaseTime());
                sysMicroShop.setExpiryTime(expiryDate);
                sysMicroShop.setVipLevel(1);
                iSysMicroShopService.updateById(sysMicroShop);
                iSysMicroShopService.clearSysMicroShopById(sysMicroShop.getId());
            }
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("店铺升级错:",e);
            throw new RuntimeException(e);
        }
    }
}
